﻿using System;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Configuration;

namespace skhukb.Util {
/// <summary>
/// CryptographyHelper의 요약 설명입니다.
/// </summary>
public class CryptographyHelper
{
    //public string strKey = ConfigurationSettings.AppSettings["CryptographyKey"];

    // Public Function
    public static string CookieEncrypt(string inStr)
    {
        return CookieEncrypt(inStr, ConfigurationManager.AppSettings["CryptographyKey"]).Replace('+', '^');
    }

    // Public Function
    public static string CookieDecrypt(string inStr) // 복호화
    {
        return CookieDecrypt(inStr.Replace('^', '+'), ConfigurationManager.AppSettings["CryptographyKey"]);
    }
    

    //문자열 암호화
    private static string CookieEncrypt(string str,string key) 
    {
          //키 유효성 검사
          byte[] btKey = ConvertStringToByteArrayA(key);

          //키가 8Byte가 아니면 예외발생
          if(btKey.Length!=8)
          {
           throw (new Exception("Invalid key. Key length must be 8 byte."));
          }

          //소스 문자열
          byte[] btSrc = ConvertStringToByteArray(str);
          DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                   
          des.Key = btKey;
          des.IV = btKey;

          ICryptoTransform desencrypt = des.CreateEncryptor();

          MemoryStream ms = new MemoryStream();

          CryptoStream cs = new CryptoStream(ms, desencrypt, 
           CryptoStreamMode.Write);

          cs.Write(btSrc, 0, btSrc.Length);
          cs.FlushFinalBlock(); 


          byte[] btEncData = ms.ToArray();

          return (ConvertByteArrayToStringB(btEncData));        
        }//end of func DesEncrypt

      

        //문자열 복호화
        private static string CookieDecrypt(string str,string key) 
        {
          //키 유효성 검사
          byte[] btKey = ConvertStringToByteArrayA(key);

          //키가 8Byte가 아니면 예외발생
          if(btKey.Length!=8)
          {
           throw (new Exception("Invalid key. Key length must be 8 byte."));
          }


          byte[] btEncData = ConvertStringToByteArrayB(str);
          DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                   
          des.Key = btKey;
          des.IV = btKey;

          ICryptoTransform desdecrypt = des.CreateDecryptor();

          MemoryStream ms = new MemoryStream();

          CryptoStream cs = new CryptoStream(ms, desdecrypt, 
           CryptoStreamMode.Write);

          cs.Write(btEncData, 0, btEncData.Length);

          cs.FlushFinalBlock(); 

          byte[] btSrc = ms.ToArray();


          return (ConvertByteArrayToString(btSrc));
          
        }//end of func DesDecrypt

        //문자열->유니코드 바이트 배열
        private static Byte[] ConvertStringToByteArray(String s)
        {
          return (new UnicodeEncoding()).GetBytes(s);
        }

        //유니코드 바이트 배열->문자열
        private static string ConvertByteArrayToString(byte[] b)
        {
          return (new UnicodeEncoding()).GetString(b, 0, b.Length);
        }

        //문자열->안시 바이트 배열
        private static Byte[] ConvertStringToByteArrayA(String s)
        {
          return (new ASCIIEncoding()).GetBytes(s);
        }

        //안시 바이트 배열->문자열
        private static string ConvertByteArrayToStringA(byte[] b)
        {
          return (new ASCIIEncoding()).GetString(b, 0, b.Length);
        }

        //문자열->Base64 바이트 배열
        private static Byte[] ConvertStringToByteArrayB(String s)
        {
          return Convert.FromBase64String(s);
        }

        //Base64 바이트 배열->문자열
        private static string ConvertByteArrayToStringB(byte[] b)
        {
          return Convert.ToBase64String(b);
        }
}
}